package com.kanshu.ksgb.fastread.doudou.base.basemvp;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.WindowManager;

import com.kanshu.ksgb.fastread.doudou.R;
import com.kanshu.ksgb.fastread.doudou.common.util.JsonUtils;
import com.kanshu.ksgb.fastread.doudou.common.business.commonbean.NotifyBean;
import com.qmuiteam.qmui.util.QMUINotchHelper;
import com.umeng.analytics.MobclickAgent;

import org.json.JSONObject;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import cn.jpush.android.api.JPushInterface;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;

/**
 * Created by PC on 2016/9/8.
 */
public abstract class BaseActivity extends AppCompatActivity {
    private static final int INVALID_VAL = -1;
    public static final String EXTRA_TYPE_SRC = "book_source";
    // 书库来源（目前就指尖书库和阅文书库）
    protected CompositeDisposable mDisposable;
    //ButterKnife
    protected Toolbar mToolbar;

    private Unbinder unbinder;
    /****************************abstract area*************************************/

    @LayoutRes
    protected abstract int getContentId();

    /************************init area************************************/
    protected void addDisposable(Disposable d){
        if (mDisposable == null){
            mDisposable = new CompositeDisposable();
        }
        mDisposable.add(d);
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
        MobclickAgent.onPageStart(getClass().getSimpleName());
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
        MobclickAgent.onPageEnd(getClass().getSimpleName());
    }

    /**
     * 配置Toolbar
     * @param toolbar
     */
    protected void setUpToolbar(Toolbar toolbar){ }

    protected void initData(Bundle savedInstanceState){ }
    /**
     * 初始化零件
     */
    protected void initWidget() { }
    /**
     * 初始化点击事件
     */
    protected void initClick(){ }
    /**
     * 逻辑使用区
     */
    protected void processLogic(){ }

    protected void setPresenter(){ }

    /*************************lifecycle area*****************************************************/

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (QMUINotchHelper.hasNotch(this)) {
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        }
        setContentView(getContentId());
        setPresenter();
        setActivityTag();
        initData(savedInstanceState);
        unbinder = ButterKnife.bind(this);
        initToolbar();
        initWidget();
        initClick();
        processLogic();

    }

    private void initToolbar(){
        //更严谨是通过反射判断是否存在Toolbar
        mToolbar = ButterKnife.findById(this, R.id.toolbar);
        if (mToolbar != null){
            supportActionBar(mToolbar);
            setUpToolbar(mToolbar);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
        if (mDisposable != null){
            mDisposable.dispose();
        }
    }

    protected void parseVipJpushChannel() {
        try {
            Uri data = getIntent().getData();
            if (data != null) {
                JSONObject object = new JSONObject(data.toString());
                String msgId = object.optString("msg_id");
                if(!TextUtils.isEmpty(msgId)){
                    JPushInterface.reportNotificationOpened(this, msgId, (byte) 2);
                }
                JSONObject jsonObject2 = object.optJSONObject("n_extras");
                String notify = jsonObject2.optString("notify");
                NotifyBean bean = JsonUtils.json2BeanByFastJson(notify, NotifyBean.class);
                parseVipJpush(bean);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    protected void parseVipJpush(NotifyBean bean){}

    /**************************used method area*******************************************/

    protected void startActivity(Class<? extends AppCompatActivity> activity){
        Intent intent = new Intent(this,activity);
        startActivity(intent);
    }

    protected ActionBar supportActionBar(Toolbar toolbar){
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
//        if (actionBar != null){
//            actionBar.setDisplayHomeAsUpEnabled(true);
//            actionBar.setDisplayShowHomeEnabled(true);
//        }
//        mToolbar.setNavigationOnClickListener(
//                (v) -> finish()
//        );
        return actionBar;
    }

    /**
     * 设置activity的tag
     */
    private void setActivityTag() {
        getWindow().getDecorView().setTag(getSpecifyTag());
    }

    /**
     * 获取Activity指定的tag
     * @return
     */
    protected String getSpecifyTag() {
        return "";
    }
}
